﻿using Engine.Utils;
using System.Collections.Generic;
using UnityEngine;

namespace Engine.UI
{
    /// <summary>
    /// 面板管理器
    /// </summary>
    public class UIManager : Singleton<UIManager>, IInit, IDispose
    {
        private Transform UIRoot;
        private List<ViewBase> popUpQueue;

        public void Init()
        {
            if (UIRoot == null)
            {
                UIRoot = new GameObject().transform;
                UIRoot.name = "UIRoot";
                popUpQueue = new List<ViewBase>();
                UnityEngine.Object.DontDestroyOnLoad(UIRoot.gameObject);
            }
        }

        /// <summary>
        /// 打开面板
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="args"></param>
        public T Show<T>() where T : ViewBase
        {
            T ui = Find<T>();
            if (ui != null)
            {
                ui.Show();
                ui.transform.SetAsLastSibling();
                SortCanvas(ui);
                return ui;
            }
            GameObject uiGo = new GameObject();
            uiGo.name = typeof(T).Name.ToString();
            uiGo.transform.SetParent(UIRoot);
            ui = uiGo.AddComponent<T>();
            popUpQueue.Add(ui);
            ui.transform.SetAsLastSibling();

            ResManager.Instance.LoadUI(ui.GetPath(), delegate (GameObject go)
            {
                if (ui != null)
                {
                    go.name = ui.name;
                    go.transform.SetParent(ui.transform);
                    go.transform.localPosition = Vector3.zero;
                    ui.OnLoaded(go.transform);
                }
                else
                {
                    GameObject.Destroy(go);
                }
                SortCanvas(ui);
            });
            return ui;
        }

        int sortingOrder = 1;

        void SortCanvas(ViewBase ui)
        {
            if (ui == null)
            {
                return;
            }
            Canvas[] uiCanvas = ui.GetComponentsInChildren<Canvas>();

            foreach (Canvas canvas in uiCanvas)
            {
                //约定 主摄像机一定是UI摄像机
                canvas.worldCamera = Camera.main;
                canvas.sortingOrder = sortingOrder;
                sortingOrder++;
            }
        }

        public T Find<T>() where T : ViewBase
        {
            T ui = null;
            for (int i = 0; i < popUpQueue.Count; i++)
            {
                if (popUpQueue[i].GetType() == typeof(T))
                {
                    ui = (T)popUpQueue[i];
                }
            }
            return ui;
        }

        public void Close(ViewBase uiView)
        {
            if (uiView == null)
            {
                return;
            }
            popUpQueue.Remove(uiView);

            ViewBase topUI = GetTopUI();
            SortCanvas(topUI);

            if (null != uiView.gameObject)
            {
                GameObject.Destroy(uiView.gameObject);
            }
            Resources.UnloadUnusedAssets();
        }

        public void Close()
        {
            ViewBase UIView = GetTopUI();
            Close(UIView);
        }

        public void CloseAll()
        {
            int max = 100;
            while (popUpQueue.Count > 0)//0
            { 
                Close();

                if (--max < 0)//0
                {
                    Log.Debug("UIManager:CloseAll()  DEAD LOOP. max=" + max);
                    break;
                }
            }
        }

        public ViewBase GetTopUI()
        {
            if (popUpQueue.Count > 0)
            {
                return popUpQueue[popUpQueue.Count - 1];
            }
            return null;
        }

        public void Dispose()
        {

        }
    }
}